<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise_封装Ajax操作</title>
</head>
<body>
<script>

    //1. 发送Ajax之前使用promise对象封装
    let p = new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest()
        xhr.open('GET','http://127.0.0.1:8000/serve')
        xhr.send()
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4){
                if (xhr.status >= 200 && xhr.status < 300){
                    resolve(xhr.response)
                }else {
                    reject(xhr.statusText)
                }
            }
        }
    })

    //待promise对象状态发生变化时，调用对应的回调函数
    p.then(function (data) {
        console.log(data)
    },function (reason) {
        console.error(reason)
    })


</script>
</body>
</html>